package cn.waka.sys.admin.controller;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import cn.waka.sys.admin.object.Node;
import cn.waka.sys.admin.service.NodeService;

@RestController
@RequestMapping("/url")  
public class UrlController extends BaseController {  
	
	@Autowired
	private NodeService nodeService;
	
    public void setNodeService(NodeService nodeService) {
		this.nodeService = nodeService;
	}

	@RequestMapping(value="index",name="获取节点信息列表")  
    public ModelAndView getUrlMapping(HttpServletRequest request) {  
        WebApplicationContext wc = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());  
        RequestMappingHandlerMapping rmhp = wc.getBean(RequestMappingHandlerMapping.class);  
        Map<RequestMappingInfo, HandlerMethod> map = rmhp.getHandlerMethods();  
        List<Map<String,String>> list=new LinkedList<Map<String,String>>();
        for(RequestMappingInfo info : map.keySet()){  
        	Map<String,String> m=new HashMap<String,String>();
        	m.put("url", info.getPatternsCondition().toString());
        	m.put("name", info.getName());
        	m.put("bean", map.get(info).getBean().toString());
        	Node node = new Node();
        	node.setTitle(info.getName());
        	node.setNode(info.getPatternsCondition().toString());
        	node.setCreate_at(new Timestamp(System.currentTimeMillis()));
        	nodeService.save(node);
        	list.add(m);
           /* System.out.println(info.getPatternsCondition().toString()  
                    + "," +info.getName() +","
                    +map.get(info).getBean().toString());  */
        }
        ModelAndView mv=new ModelAndView();
        mv.addObject("nodes", list);
        return mv;
    }  
}